package models

import (
	"cubeAdmin/dto"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

type SysDict struct {
	Id     int64  `json:"id"`
	Name   string `json:"name" valid:"Required;"`
	Remark string `json:"remark" valid:"Required;"`
	BaseModel
}

func init() {
	orm.RegisterModel(new(SysDict))
}

// GetDictById 通过Id获取字典信息
// GetDictById retrieves GetDict by Id. Returns error if
// Id doesn't exist
func GetDictById(id int64) (v *SysDict, err error) {
	o := orm.NewOrm()
	v = &SysDict{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// get all
func GetAllDict(base dto.BasePage, query ...interface{}) (int, []SysDict) {
	var (
		tableName = "sys_dict"
		dicts     []SysDict
		condition = ""
	)
	if base.Blurry != "" {
		condition = " and name= '" + base.Blurry + "'"
	}
	logs.Info(base)
	total, _, rs := GetPagesInfo(tableName, base.Page, base.Limit, condition)
	rs.QueryRows(&dicts)
	return total, dicts
}

// last inserted Id on success.
func AddDict(m *SysDict) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

func UpdateByDict(m *SysDict) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m)
	return
}

func DelByDict(id int64) (err error) {
	o := orm.NewOrm()
	_, err = o.Raw("UPDATE sys_dict SET is_del = ? WHERE id = ?", 1, id).Exec()
	return
}
